package freaks.terah.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import freaks.terah.model.Observer;
import freaks.terah.model.PlayerModel;
import freaks.terah.model.Unit;
import javax.swing.border.EtchedBorder;
import java.awt.Color;

public class UnitFrame extends JPanel implements ActionListener, ListSelectionListener, Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JList ownedList;
	private JList buildableList;
	private JList builtList;
	private JList salvagedList;
	private JButton btnBuild;
	private JButton btnCancel;
	private JButton btnUnsalvage;
	private JButton btnSalvage;
	
	private PlayerModel theModel;
	private RessourceCountPanel outputPanel;
	private RessourceCountPanel upkeepPanel;
	private JLabel txtName;
	private RessourceCountPanel costPanel;
	private JTextPane txtDescription;
	/**
	 * Create the panel.
	 */
	public UnitFrame(PlayerModel model) {
		theModel = model;
		theModel.addListener(this);
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Edit Units", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel.setBounds(6, 6, 419, 500);
		add(panel);
		panel.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(242, 59, 154, 158);
		panel.add(scrollPane);
		
		buildableList = new JList();
		buildableList.setModel(model.getBuildableUnits());
		buildableList.addListSelectionListener(this);
		buildableList.setCellRenderer(theModel.getAPRenderer());
		scrollPane.setViewportView(buildableList);
		
		JLabel lblNewLabel = new JLabel("Buildable");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(241, 31, 155, 16);
		panel.add(lblNewLabel);
		
		btnBuild = new JButton("<-- Build");
		btnBuild.setBounds(263, 229, 117, 29);
		btnBuild.addActionListener(this);
		panel.add(btnBuild);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(31, 59, 154, 158);
		panel.add(scrollPane_1);
		
		ownedList = new JList();
		ownedList.setModel(model.getOwnedUnits());
		ownedList.addListSelectionListener(this);
		ownedList.setCellRenderer(theModel.getAPRenderer());
		scrollPane_1.setViewportView(ownedList);
		
		JLabel lblOwned = new JLabel("Owned");
		lblOwned.setHorizontalAlignment(SwingConstants.CENTER);
		lblOwned.setBounds(30, 31, 155, 16);
		panel.add(lblOwned);
		
		btnSalvage = new JButton("Kill Off -->");
		btnSalvage.setBounds(52, 229, 117, 29);
		btnSalvage.addActionListener(this);
		panel.add(btnSalvage);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(242, 301, 154, 84);
		panel.add(scrollPane_2);
		
		salvagedList = new JList();
		salvagedList.setModel(model.getDestroyedUnits());
		salvagedList.addListSelectionListener(this);
		salvagedList.setCellRenderer(theModel.getRessourceRenderer());
		scrollPane_2.setViewportView(salvagedList);
		
		JLabel lblSalvaged = new JLabel("Killed off");
		lblSalvaged.setHorizontalAlignment(SwingConstants.CENTER);
		lblSalvaged.setBounds(241, 273, 155, 16);
		panel.add(lblSalvaged);
		
		btnUnsalvage = new JButton("<-- Cancel");
		btnUnsalvage.setBounds(263, 397, 117, 29);
		btnUnsalvage.addActionListener(this);
		panel.add(btnUnsalvage);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(31, 301, 154, 84);
		panel.add(scrollPane_3);
		
		builtList = new JList();
		builtList.setModel(model.getNewUnits());
		builtList.addListSelectionListener(this);
		builtList.setCellRenderer(theModel.getRessourceRenderer());
		scrollPane_3.setViewportView(builtList);
		
		JLabel lblBuilt = new JLabel("Built");
		lblBuilt.setHorizontalAlignment(SwingConstants.CENTER);
		lblBuilt.setBounds(30, 273, 155, 16);
		panel.add(lblBuilt);
		
		btnCancel = new JButton("Cancel -->");
		btnCancel.setBounds(52, 397, 117, 29);
		btnCancel.addActionListener(this);
		panel.add(btnCancel);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Info", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(437, 6, 267, 500);
		add(panel_2);
		panel_2.setLayout(null);
		
		JLabel label = new JLabel("Name:");
		label.setHorizontalAlignment(SwingConstants.TRAILING);
		label.setBounds(17, 29, 90, 16);
		panel_2.add(label);
		
		JLabel label_1 = new JLabel("Description:");
		label_1.setHorizontalAlignment(SwingConstants.TRAILING);
		label_1.setBounds(17, 61, 90, 16);
		panel_2.add(label_1);
		
		txtDescription = new JTextPane();
		txtDescription.setEditable(false);
		txtDescription.setBackground(UIManager.getColor("Button.background"));
		txtDescription.setBounds(107, 61, 154, 89);
		panel_2.add(txtDescription);
		
		outputPanel = new RessourceCountPanel();
		outputPanel.setBounds(107, 241, 116, 66);
		panel_2.add(outputPanel);
		
		upkeepPanel = new RessourceCountPanel();
		upkeepPanel.setBounds(107, 319, 116, 66);
		panel_2.add(upkeepPanel);
		
		JLabel label_2 = new JLabel("Cost:");
		label_2.setHorizontalAlignment(SwingConstants.TRAILING);
		label_2.setBounds(17, 189, 90, 16);
		panel_2.add(label_2);
		
		JLabel label_3 = new JLabel("Output:");
		label_3.setHorizontalAlignment(SwingConstants.TRAILING);
		label_3.setBounds(17, 267, 90, 16);
		panel_2.add(label_3);
		
		JLabel label_4 = new JLabel("Upkeep cost:");
		label_4.setHorizontalAlignment(SwingConstants.TRAILING);
		label_4.setBounds(17, 345, 92, 16);
		panel_2.add(label_4);
		
		costPanel = new RessourceCountPanel();
		costPanel.setBounds(107, 163, 116, 66);
		panel_2.add(costPanel);
		
		txtName = new JLabel("");
		txtName.setBounds(107, 29, 154, 16);
		panel_2.add(txtName);

		btnCancel.setEnabled(false);
		btnBuild.setEnabled(false);
		btnUnsalvage.setEnabled(false);
		btnSalvage.setEnabled(false);
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		JList list = (JList) arg0.getSource();
		int index = list.getSelectedIndex();
		if (index != -1)
		{
			Unit unit = (Unit) list.getModel().getElementAt(index);
			
			btnBuild.setEnabled(false);
		    btnSalvage.setEnabled(false);
			
			if (theModel.canBuy(unit))
			{
			    btnBuild.setEnabled(true);
			    btnSalvage.setEnabled(true);
			}
			
			btnCancel.setEnabled(true);
			btnUnsalvage.setEnabled(true);
			if (list != ownedList)
			{
				ownedList.clearSelection();
				btnSalvage.setEnabled(false);
			}
			if (list != salvagedList)
			{
				salvagedList.clearSelection();
				btnUnsalvage.setEnabled(false);
			}
			if (list != buildableList)
			{
				buildableList.clearSelection();
				btnBuild.setEnabled(false);
			}
			if (list != builtList)
			{
				builtList.clearSelection();
				btnCancel.setEnabled(false);
			}
			
			txtName.setText(unit.getName());
			txtDescription.setText(unit.getDescription());
			costPanel.setRessource(unit.getCost(), null);
			outputPanel.setRessource(unit.getOutput(), null);
			upkeepPanel.setRessource(unit.getUpkeep(), null);
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == btnBuild && buildableList.getSelectedIndex() != -1)
		{
			theModel.doAction(theModel.ACTION_CREATE_UNIT, buildableList.getSelectedIndex());
			
	        btnBuild.setEnabled(false);
	        btnCancel.setEnabled(true);
	        builtList.setSelectedIndex(theModel.getNewUnits().getSize()-1);
	        
		} else if (arg0.getSource() == btnSalvage && ownedList.getSelectedIndex() != -1)
		{
			theModel.doAction(theModel.ACTION_SALVAGE_UNIT, ownedList.getSelectedIndex());
			
			btnSalvage.setEnabled(false);
	        btnUnsalvage.setEnabled(true);
	        salvagedList.setSelectedIndex(theModel.getDestroyedUnits().getSize()-1);
		} else if (arg0.getSource() == btnCancel && builtList.getSelectedIndex() != -1)
		{
			theModel.doAction(theModel.ACTION_CANCEL_CREATE_UNIT, builtList.getSelectedIndex());
			
	        btnCancel.setEnabled(false);
	        btnBuild.setEnabled(true);
	        buildableList.setSelectedIndex(theModel.getBuildableUnits().getSize()-1);
	        
		} else if (arg0.getSource() == btnUnsalvage && salvagedList.getSelectedIndex() != -1)
		{
			theModel.doAction(theModel.ACTION_CANCEL_SALVAGE_UNIT, salvagedList.getSelectedIndex());
			
			btnUnsalvage.setEnabled(false);
			btnSalvage.setEnabled(true);
			ownedList.setSelectedIndex(theModel.getOwnedUnits().getSize()-1);
		}
	}

	@Override
	public void notifyMe() {
		try {
		    if (ownedList.getSelectedIndex() != -1)
		    	if (!theModel.canBuy(((Unit)ownedList.getSelectedValue())))
			        btnSalvage.setEnabled(false);
		    	else
		    		btnSalvage.setEnabled(true);
		} catch (Exception e) {
			btnSalvage.setEnabled(false);
		}
		
		try {
		    if (buildableList.getSelectedIndex() != -1)
		    	if (!theModel.canBuy(((Unit)buildableList.getSelectedValue())))
			        btnBuild.setEnabled(false);	
		    	else
		    		btnBuild.setEnabled(true);
		} catch (Exception e) {
			btnBuild.setEnabled(false);
		}
	}

}
